<?php

namespace Fendx\Funcs;


use Ramsey\Uuid\Uuid;

class FendxUtil
{

    /**
     * 判断变量是否为空
     * 特殊的地方在于，0 '0' '000' 也会被判定非空
     * @param $param
     * @return bool
     */
    public static function realEmpty($param)
    {
        if ($param === null
            || (is_array($param) && empty($param))
            || (is_string($param) && $param === '')) {
            return true;
        }
        return false;
    }

    public static function loadFiles(array $files) {
        foreach ($files as $file) {
            if (file_exists(SRC_ROOTDIR . $file)) {
                require_once SRC_ROOTDIR . $file;
            }
        }
    }

    /**
     * 生成唯一工单ID
     */
    public static function createUniqueIdWork($redis = null): string {
        static $idWork = null;
        if (is_null($idWork)) {
            $idWork = new \Fendx\Tools\IdWork(rand(1, 1023));
        }
        $key = $idWork->nextId();

        if (!is_null($redis)) {
            $redis->multi();
            $redis->setNX($key, 1);
            $redis->expire($key, 120);
            $ret = $redis->exec();

            if (!$ret[0]) {
                return self::createUniqueIdWork($redis);
            }
        }
        return (string) $key;
    }

    /**
     * 生成MD5-Hash
     *
     * @param        $string
     * @param string $namespace
     *
     * @return \Ramsey\Uuid\UuidInterface
     */
    public static function encyptMD5Hash($string, $namespace = '')
    {

        if (empty($namespace)) {
            $namespace = Uuid::NAMESPACE_DNS;
        }

        return Uuid::uuid3($namespace, $string);
    }

    /**
     * 生成SHA1-Hash
     *
     * @param        $string
     * @param string $namespace
     *
     * @return \Ramsey\Uuid\UuidInterface
     */
    public static function encyptHash($string, $namespace = '')
    {

        if (empty($namespace)) {
            $namespace = Uuid::NAMESPACE_DNS;
        }

        return Uuid::uuid5($namespace, $string);
    }

    /**
     * 生成时间Hash
     *
     * @return \Ramsey\Uuid\UuidInterface
     */
    public static function encyptTimeHash()
    {

        return Uuid::uuid1();
    }

    /**
     * 生成随机Hash
     *
     *
     * @return \Ramsey\Uuid\UuidInterface
     */
    public static function encyptRandomHash()
    {

        return Uuid::uuid4();
    }

    /**
     * 生成密码
     *
     */
    public static function generatePassword(string $password, string $salt = 'fendx') {
        return md5(md5($password) . $salt);
    }

    /**
     * 生成sha256密码
     */
    public static function generateSha256Password(string $password, string $salt = 'fendx') {
        return hash('sha256', "password=".$password."&salt=".$salt);
    }

    /**
     * 获得指定位数的随机字符串
     *
     * @param $length
     *
     * @return string
     */
    public static function generateRandomString($length)
    {

        $num = 'abcdefghijklmnopqrstuvwxyz1234567890';
        $ret = '';
        $numLen = strlen($num) - 1;
        for ($i = 1; $i < $length; $i++) {
            $ret .= $num[rand(0, $numLen)];
        }

        return $ret;
    }
}
